/**
 * Created by haowanh5 on 2016/11/1.
 */

"use strict";

const Redis = require("redis");
const Logger = require("pomelo-logger").getLogger("RedisConnector", __filename);
const Configs = require("../Configs/RedisServer");
const IPPort = require("../Utils/IPPortService");

var RedisClient = Redis.createClient(Configs.ClusterServer.port, Configs.ClusterServer.host, Configs.ClusterServer.config);
RedisClient.on("ready", () => {
    Logger.info("Redis connect ready");
});
RedisClient.on("end", () => {
    Logger.info("Redis connect end");
});
RedisClient.on("error", function (err) {
    Logger.info("Redis connect error : " + err);
});

var RedisPubSub = Redis.createClient(Configs.PubSubServer.port, Configs.PubSubServer.host, Configs.PubSubServer.config);
RedisPubSub.on("ready", () => {
    Logger.info("Redis pub/sub connect ready");
    RedisPubSub.subscribe(IPPort.getIpPort());
    //新邮件推送
    RedisPubSub.subscribe("mail");
    //新配置文件更新推送
    RedisPubSub.subscribe("updateConfig");
});
RedisPubSub.on("end", () => {
    Logger.info("Redis pub/sub connect end");
});
RedisPubSub.on("error", (err) => {
    Logger.info("Redis pub/sub error : " + err);
});

var RedisPub = Redis.createClient(Configs.PubSubServer.port, Configs.PubSubServer.host, Configs.PubSubServer.config);
RedisPub.on("ready", () => {
    Logger.info("Redis pub connect ready");
});
RedisPub.on("end", () => {
    Logger.info("Redis pub connect end");
});
RedisPub.on("error", (err) => {
    Logger.info("Redis pub error : " + err);
});

// Client For Pub Can Only Publish, For Sub Can Only Subscribe, For Data Store Cannot Sub And Sub
module.exports = {
    getRedisClient: function () {
        return RedisClient;
    },
    getRedisPubSub: function () {
        return RedisPubSub;
    },
    getRedisSub: function () {
        return RedisPub;
    }
};


